<template>
  <div class="vocation">
    <el-form ref="form" :model="form" label-width="100px" class="firstForm">
      <el-form-item label="行业名称：" prop="name">
        <el-input v-model.trim="form.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item class="isRight">
        <el-button type="primary" size="small">查询</el-button>
        <el-button size="small" @click="btnReset('form')">重置</el-button>
        <el-button type="text">展开</el-button>
      </el-form-item>
    </el-form>
    <el-tag>
      <i class="el-icon-info" />
      <div>
        已选择 <span>{{ 4 }}</span> 项 服务调用总计：<span>{{ 36.4 }}</span>万 <span>清空</span>
      </div>
    </el-tag>
    <el-table :data="List" style="width: 100%;">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="行业名称" sortable width="600" />
      <el-table-column prop="described" label="描述" width="800" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="editData(scope.$index, scope.row)"
          />
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            prop="id"
            @click="delData(scope.$index, scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <div class="add">
      +新增
    </div>
    <el-pagination
      :current-page="page.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="page.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <div>
      <el-dialog title="编辑行业信息" :visible.sync="dialogVisible" width="25%" @close="btnCancel">
        <span slot="default">
          <el-form ref="editForm" :rules="rules" :model="editForm" label-width="80px">
            <el-form-item prop="name" label="行业名称">
              <el-input v-model="editForm.name" />
            </el-form-item>
            <el-form-item prop="described" label="描述">
              <el-input v-model="editForm.described" />
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="btnCancel">取 消</el-button>
          <el-button type="primary" @click="onSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getVocation, getVocationPage, delVocation, editVocation } from '@/api/setting';

export default {
  name: 'Vocation',
  data() {
    return {
      dialogVisible: false,
      form: {
        // 需要查询的名字
        name: '',
      },
      editForm: {
        name: '',
        described: '',
      },
      page: {
        page: 1,
        pagesize: 5,
        total: 0,
      },
      // 表格数据存放在这
      List: [],
      multipleSelection: [],
      rules: {
        name: [
          { required: true, message: '请输入行业名称', trigger: 'blur' },
          { min: 1, max: 20, message: '文字请控制在1~20字以内', trigger: 'blur' },
        ],
        described: [
          { required: true, message: '请输入描述内容', trigger: 'blur' },
          { min: 1, max: 20, message: '描述内容请控制在1~20字以内', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.getVocation();
    this.getVocationPage();
  },
  methods: {
    // formatter(row, column) {
    //   return row.operation;
    // },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      // 每页条数动态绑定
      this.page.pagesize = val;
      this.getVocationPage();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      // 当前页动态绑定
      this.page.page = val;
      this.getVocationPage();
    },
    // 总共多少条数据
    async getVocation() {
      const { data } = await getVocation();
      // console.log(data);
      // 总条数等于data数据的长度
      this.page.total = data.length;
    },
    // 分页功能
    async getVocationPage() {
      const { data } = await getVocationPage({
        _page: this.page.page,
        _limit: this.page.pagesize,
      });
      // console.log(data);
      // 把数据放入数组中显示出来
      this.List = data;
    },
    // 删除数据
    delData(index, row) {
      this.$confirm('确定删除该部门吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // console.log(index, row);
          // console.log(row.id);
          return delVocation(row.id);
        })
        .then(() => {
          this.getVocationPage();
          this.getVocation();
          this.$message.success('删除成功');
        })
        .catch(() => {
          this.$message.warning('删除失败');
        });
    },
    // 弹出对话框，传入数据
    editData(index, row) {
      this.editForm = row;
      this.dialogVisible = true;
    },
    // 编辑
    onSubmit() {
      this.$refs.editForm.validate(async(valid) => {
        if (valid) {
          await editVocation(this.editForm, this.editForm.id).then((res) => {
            // console.log(res);
            if (res.status === 200) {
              this.$message.success('编辑成功');
              this.dialogVisible = false;
            } else {
              this.$message.warning('编辑失败');
            }
          });
        } else {
          return false;
        }
      });
    },
    // 关闭对话框重置表单
    btnCancel() {
      this.dialogVisible = false;
      this.getVocationPage();
    },
    // 重置按钮
    btnReset(form) {
      this.$refs[form].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.vocation {
  ::v-deep .firstForm {
    margin-top: 30px;
    display: flex;
    justify-content: space-between;
    label {
      font-weight: 400;
    }
    .el-input__inner {
      width: 264px;
      height: 32px;
    }
    .isRight {
      position: relative;
      right: 200px;
      bottom: 0;

      .unfold {
        margin-left: 8px;
        color: #1890ff;
      }
    }
  }
  ::v-deep .el-tag {
    margin-bottom: 20px;
    width: 100%;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    div {
      margin-left: 10px;
      display: inline-block;
      color: rgba(0, 0, 0, 0.647058823529412);
      span:nth-child(1),
      span:nth-child(3) {
        color: #1890ff;
      }
      span:nth-child(2) {
        font-weight: 700;
      }
    }
  }
  .add {
    margin-top: 30px;
    width: 100%;
    height: 50px;
    border: 1px dashed #27a6fd;
    border-radius: 10px;
    text-align: center;
    line-height: 50px;
    color: #27a6fd;
    background-color: #fafafa;
    font-size: 14px;
    font-weight: 700;
  }
  .el-pagination {
    float: right;
    overflow: hidden;
    margin-top: 20px;
  }
}
</style>
